﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

using umbraco.interfaces;
using umbraco.IO;

namespace DigibizAdvancedMediaPicker
{
    public class DAMP_DataEditor : PlaceHolder, IMediaPicker
    {
        private string _usercontrolPath = IOHelper.ResolveUrl(SystemDirectories.Umbraco) + "/plugins/DigibizAdvancedMediaPicker/DigibizAdvancedMediaPicker.ascx";
        private object _value;
        
        #region Properties

        private bool FirstLoad
        {
            get
            {
                if (ViewState["FirstLoad"] == null)
                {
                    ViewState["FirstLoad"] = true;
                }

                return Convert.ToBoolean(ViewState["FirstLoad"]);
            }
            set
            {
                ViewState["FirstLoad"] = value;
            }
        }

        public string SaveValue
        {
            get;
            set;
        }

        public string AllowedExtensionsValue
        {
            get;
            set;
        }

        public string AllowedSelectableMediaTypesValue
        {
            get;
            set;
        }

        public string AllowedCreateableMediaTypesValue
        {
            get;
            set;
        }

        public int DefaultMediaTypeValue
        {
            get;
            set;
        }

        public bool HideCreateValue
        {
            get;
            set;
        }

        public int StartNodeIdValue
        {
            get;
            set;
        }

        public bool SelectMultipleNodesValue
        {
            get;
            set;
        }

        public int MinimumNodesValue
        {
            get;
            set;
        }

        public int MaximumNodesValue
        {
            get;
            set;
        }

        public string CropPropertyAliasValue
        {
            get;
            set;
        }

        public string CropNameValue
        {
            get;
            set;
        }

        public int ThumbnailWidthValue
        {
            get;
            set;
        }

        public int ThumbnailHeightValue
        {
            get;
            set;
        }

        public bool HideEditValue
        {
            get;
            set;
        }

        public bool HideOpenValue
        {
            get;
            set;
        }

        public bool HidePixlrValue
        {
            get;
            set;
        }

        public bool Mandatory
        {
            get;
            set;
        }

        public bool CheckValidation
        {
            get;
            set;
        }

        public string MinimumNodesText
        {
            get;
            set;
        }

        public string MaximumNodesText
        {
            get;
            set;
        }

        public string MandatoryText
        {
            get;
            set;
        }

        public bool EnableSearch
        {
            get;
            set;
        }

        public bool EnableSearchAutoSuggest
        {
            get;
            set;
        }

        public string SearchMethod
        {
            get;
            set;
        }

        public int DataTypeDefinitionId
        {
            get;
            set;
        }

        public object Value
        {
            get
            {
                return ((IMediaPicker)Controls[0]).Value;
            }
            set
            {
                _value = value;
            }
        }

        public object ValueUC
        {
            set
            {
                ((IMediaPicker)Controls[0]).Value = value;
            }
        }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.Controls.Add(new UserControl().LoadControl(_usercontrolPath));
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ((IMediaPicker)Controls[0]).SaveValue = SaveValue;
            ((IMediaPicker)Controls[0]).AllowedExtensionsValue = AllowedExtensionsValue;
            ((IMediaPicker)Controls[0]).AllowedSelectableMediaTypesValue = AllowedSelectableMediaTypesValue;
            ((IMediaPicker)Controls[0]).AllowedCreateableMediaTypesValue = AllowedCreateableMediaTypesValue;
            ((IMediaPicker)Controls[0]).DefaultMediaTypeValue = DefaultMediaTypeValue;
            ((IMediaPicker)Controls[0]).HideCreateValue = HideCreateValue;
            ((IMediaPicker)Controls[0]).StartNodeIdValue = StartNodeIdValue;
            ((IMediaPicker)Controls[0]).SelectMultipleNodesValue = SelectMultipleNodesValue;
            ((IMediaPicker)Controls[0]).MinimumNodesValue = MinimumNodesValue;
            ((IMediaPicker)Controls[0]).MaximumNodesValue = MaximumNodesValue;
            ((IMediaPicker)Controls[0]).CropPropertyAliasValue = CropPropertyAliasValue;
            ((IMediaPicker)Controls[0]).CropNameValue = CropNameValue;
            ((IMediaPicker)Controls[0]).ThumbnailWidthValue = ThumbnailWidthValue;
            ((IMediaPicker)Controls[0]).ThumbnailHeightValue = ThumbnailHeightValue;
            ((IMediaPicker)Controls[0]).HideEditValue = HideEditValue;
            ((IMediaPicker)Controls[0]).HideOpenValue = HideOpenValue;
            ((IMediaPicker)Controls[0]).HidePixlrValue = HidePixlrValue;
            ((IMediaPicker)Controls[0]).Mandatory = Mandatory;
            ((IMediaPicker)Controls[0]).CheckValidation = CheckValidation;
            ((IMediaPicker)Controls[0]).MinimumNodesText = MinimumNodesText;
            ((IMediaPicker)Controls[0]).MaximumNodesText = MaximumNodesText;
            ((IMediaPicker)Controls[0]).MandatoryText = MandatoryText;
            ((IMediaPicker)Controls[0]).EnableSearch = EnableSearch;
            ((IMediaPicker)Controls[0]).EnableSearchAutoSuggest = EnableSearchAutoSuggest;
            ((IMediaPicker)Controls[0]).SearchMethod = SearchMethod;
            ((IMediaPicker)Controls[0]).DataTypeDefinitionId = DataTypeDefinitionId;

            if (FirstLoad)
            {
                //The value only needs to be set the first time the page is loaded.
                //If the value is set multiple times the user will loose it's selected items after pressing the save button.
                //We don't use Page.IsPostBack for this because the control could also be loaded the first time on a postback (with Canvas).
                ((IMediaPicker)Controls[0]).Value = _value;

                FirstLoad = false;
            }
        }
    }
}